class Admin::ArticlesController < Admin::BaseController

  verify :method => :post, :only => [:create, :update, :destroy],
         :redirect_to => {:action => 'list'}

  def index
    list
    render :action => 'list'
  end
  
  def list
    @articles = Article.paginate_all_by_lough_id(current_lough.id, 
                                                :page => params[:page],
                                                :order => 'published_at DESC')
  end

  def new
    @tiny_mce_used = true
    @article = Article.new
  end
  
  def create
    @article = current_user.with_options(default_article_options) do |user|
      user.add_article(params[:article].symbolize_keys)
    end
    
    #puts "article.lough.id: #{@article.lough.id}"
    
    if @article.save
      flash[:notice] = 'Success.'
      redirect_to index_url
    else
      render :action => 'new'
    end
  end

  def edit
    @tiny_mce_used = true
    if params[:id]
      @article = Article.find_by_id(params[:id])
    else
      redirect_to :action => 'list'
    end
  end
  
  def update
    @article = Article.find_by_id(params[:id])
    if @article.update_attributes(params[:article].symbolize_keys)
      redirect_to index_url
    else
      render :action => 'edit'
    end
  end
  
  def destroy
    Article.find_by_id(params[:id]).destroy 
    redirect_to index_url
  end
  
  protected 
  
  def default_article_options
    return {:lough => current_lough}
  end
    
end
